<?php
/*========================================= JooSam =========================================/
/																							/
/	Extention:		JooSam																	/
/	Created by:		Broadcastweb.eu - Paules												/
/	Site:			http://www.broadcastweb.eu												/
/	Email:			support@broadcastweb.eu													/
/	Copyright:		2011 - 2012 (c) Broadcastweb.eu											/
/	License:		CC BY-NC-ND 3.0 ~ http://creativecommons.org/licenses/by-nc-nd/3.0/		/
/	File Version:	0003																	/
/																							/
/========================================== JooSam ========================================*/
defined('_JEXEC') or die;

class JooSamHelper{

	public static function addSubmenu($vName){
	
		$url = 'index.php?option=com_joosam&';
		JSubMenuHelper::addEntry(JText::_('COM_JOOSAM_MENU_JOOSAM'),$url.'view=joosam',$vName == 'joosam');				// JooSam
        JSubMenuHelper::addEntry(JText::_('COM_JOOSAM_MENU_SERVER'),$url.'view=servers',$vName == 'servers');			// Server
		JSubMenuHelper::addEntry(JText::_('COM_JOOSAM_MENU_REQEVT'),$url.'view=reqevts',$vName == 'reqevts');			// Events & Requests
		JSubMenuHelper::addEntry(JText::_('COM_JOOSAM_MENU_TXTSTR'),$url.'view=textcodes',$vName == 'textcodes');		// Text codes
		JSubMenuHelper::addEntry(JText::_('COM_JOOSAM_MENU_SAMADMIN'),$url.'view=samadmin',$vName == 'samadmin');		// SAM Admin
	}
		

	public static function getActions(){

		$user	= JFactory::getUser();
		$result	= new JObject;
		$actions = array('core.admin', 'core.manage', 'core.create', 'core.edit', 'core.edit.state', 'core.delete');
		foreach ($actions as $action){
			$result->set($action,	$user->authorise($action, 'com_joosam'));
		}
		return $result;
	}

	public static function jsmCrypt($type, $string){

		$jConf = JFactory::getApplication();
		$key = $jConf->getCfg('secret');
		$key = md5($key);
		$password = $string;
		if(function_exists('mcrypt_module_open')){
			$cipher = MCRYPT_RIJNDAEL_256;
			$mode = MCRYPT_MODE_CBC;
			$iv = md5($key);
			$encrypt = base64_encode(mcrypt_encrypt($cipher, $key, $password, $mode, $iv));
			$decrypt = rtrim(mcrypt_decrypt($cipher, $key, base64_decode($password), $mode, $iv));
		}
		else{
			$encrypt = base64_encode($password);
			$decrypt = base64_decode($password);
		}

		if($type == 'decrypt'){ return $decrypt; }
		else if($type == 'encrypt'){ return $encrypt; }
		else{ return false; }
	}

	public static function settings($option, $dbid){

		$db =& JFactory::getDBO();
		
		// Get MySQL Settings
		if($option == 'mysql'){
			$query = "	SELECT sql_ip, sql_port, sql_user, sql_pass, sql_db
						FROM ".$db->nameQuote('#__joosam_server')."
						WHERE ".$db->nameQuote('id')." = ".$dbid.";";
			$db->setQuery($query);
			$row = $db->loadAssoc();
			if($row){
				$pass = $row['sql_pass'];
				$port = $row['sql_port'];
				if($pass != ''){ $pass = JooSamHelper::jsmCrypt('decrypt', $pass); }
				if($port == ''){ $port = '3306'; }
				$result = array(
							'server' => $row['sql_ip'],
							'port' => $port,
							'user' => $row['sql_user'],
							'pass' => $pass,
							'db' => $row['sql_db'] );
				return $result;
			}
			else{
				return false;
			}
		}
		// Get SAM settings
		else if($option == 'sam'){
			$query = "	SELECT sam_ip, sam_port
						FROM ".$db->nameQuote('#__joosam_server')."
						WHERE ".$db->nameQuote('id')." = ".$dbid.";";
			$db->setQuery($query);
			$row = $db->loadAssoc();
			if($row){
				$result = array(
							'server' => $row['sam_ip'],
							'port' => $row['sam_port'] );
				return $result;
			}
			else{
				return false;
			}
		}
		else if($option == 'image'){
			$query = "	SELECT image_type, image_data
						FROM ".$db->nameQuote('#__joosam_server')."
						WHERE ".$db->nameQuote('id')." = ".$dbid.";";
			$db->setQuery($query);
			$row = $db->loadAssoc();
			if($row){
				$result = array(
							'type' => $row['image_type'],
							'data' => $row['image_data'] );
				return $result;
			}
			else{
				return false;
			}
		}
		else{
			return false;
		}
	}

	public static function backwork(){

		$doc = JFactory::getDocument();
		$doc->addStyleSheet('components/com_joosam/extras/css/admin.css');
	}

	public function getPicture($inpartist, $data, $sid){

		$conf = JooSamHelper::settings('image', $sid);
		$source = $conf['type'];
		$option = $conf['data'];

		// LastFM
		if($source == '0' and $option != ''){
			$lastfm = array(
						'url' => 'http://ws.audioscrobbler.com/2.0/',
						'method' => 'artist.getinfo',
						'data' => 'artist='.urlencode($inpartist),
						'ApiKey' => $option
			);
			$url = $lastfm['url'].'?method='.$lastfm['method'].'&'.$lastfm['data'].'&api_key='.$lastfm['ApiKey'];
			
			if(function_exists('curl_init')){
				$srv = curl_init($url);
				curl_setopt($srv, CURLOPT_HEADER, false);
				curl_setopt($srv, CURLOPT_RETURNTRANSFER, true);
				$srvData = curl_exec($srv);
				curl_close($srv);
				if($srvData == ''){
					$error = 'connect';
					$image = JText::_("COM_JOOSAM_ERROR_LASTFM_CONNECT");
				}
				else{
					$lastferrorcd = array();
					preg_match('/\d\d\d/', $srvData, $lastferrorcd);
					$lastferrorcd[] = 'placeholder';
					if($lastferrorcd[0] == 404){
						$error = 'last404';
						$image = JText::_("COM_JOOSAM_ERROR_LASTFM_404");
					}
					else{
						$xml = simplexml_load_string($srvData);
						$status = $xml->attributes()->status;
						if($status == 'ok'){
							$image = $xml->artist[0]->image[4];
							if($image == ''){$image = $xml->artist[0]->image[3];}
							if($image == ''){$image = $xml->artist[0]->image[2];}
							if($image == ''){$image = $xml->artist[0]->image[1];}
							if($image == ''){$image = 'noimage';}
							if($image == 'noimage'){
								$image = JText::_("COM_JOOSAM_ERROR_LASTFM_NOIMGFOUND");
								$error = 'noimage';
							}
							else{
								$error = 'passed';
							}
						}
						else if($status == 'failed'){
							$error = 'lastfmerr';
							$image = $xml->error;
						}
						else{
							$error = 'changed';
							$image = JText::_("COM_JOOSAM_ERROR_LASTFM_UNKNOWN_RESPONSE");
						}
					}
				}
			}
			else{
				$error = 'nocurl';
				$image = JText::_("COM_JOOSAM_ERROR_PHP_NOCURL");
			}
		}

		// Joomla Image
		else if($source == '1' and $option != ''){
			
			$imgdata = $option.$data;
			if(file_exists($imgdata)){
				$error = 'passed';
				$image = $imgdata;
			}
			else{
				$error = 'noimage';
				$image = $imgdata;
			}
		}
		else{
			$error = 'notypedata';
			$image = JText::_("COM_JOOSAM_ERROR_IMAGE_SEARCH");
		}
		$result = array(
					'error' => $error,
					'image' => $image
					);
		
		return $result;
	}

	public static function currSrvActive(){
		$db =& JFactory::getDBO();
		$query = "SELECT id, srv_name FROM ".$db->nameQuote('#__joosam_server')." WHERE ".$db->nameQuote('srv_active')." = 1";
		$db->setQuery($query);
		$row = $db->loadAssoc();
		if($row){
			$data = array(
					'id' => $row['id'],
					'name' => $row['srv_name']
					);
			return $data;
		}
		else{
			return false;
		}
	}

	public static function currSrvEdit(){
		$db =& JFactory::getDBO();
		$query = "SELECT id, srv_name FROM ".$db->nameQuote('#__joosam_server')." WHERE ".$db->nameQuote('set_admin')." = 1";
		$db->setQuery($query);
		$row = $db->loadAssoc();
		if($row){
			$data = array(
					'id' => $row['id'],
					'name' => $row['srv_name']
					);
			return $data;
		}
		else{
			return false;
		}
	}
}